package com.gxa.community.admin.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import org.springframework.jms.core.JmsMessagingTemplate;

import javax.jms.ConnectionFactory;

@Configuration
public class ActiveMQConfig {
    @Value("${spring.activemq.broker-url}")
    private String brokerUrl;
    @Value("${spring.activemq.user}")
    private String username;
    @Value("${spring.activemq.password}")
    private String password;


    @Bean
    public ConnectionFactory connectionFactory() {
        return new ActiveMQConnectionFactory(username, password,
                brokerUrl);
    }

    @Bean("MyjmsTemplate")
    public JmsMessagingTemplate jmsMessageTemplate() {
        return new JmsMessagingTemplate(connectionFactory());
    }

    //监听器容器工厂配置
    @Bean
    public SimpleJmsListenerContainerFactory containerFactory(ConnectionFactory connectionFactory){
        SimpleJmsListenerContainerFactory containerFactory = new SimpleJmsListenerContainerFactory();
        containerFactory.setSessionTransacted(false);//关闭事务
        containerFactory.setSessionAcknowledgeMode(4);//4表示单条消息确认（逐条签收）
        containerFactory.setConnectionFactory(connectionFactory);
        return containerFactory;
    }
}
